<template>
	<el-dialog  v-model="show" :title="comTitle(type)" width="500" align-center>
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon>
            <el-form-item label="广告名称" prop="name">
                <el-input v-model="ruleForm.name" placeholder="请输入名称"/>
            </el-form-item>
            <el-form-item label="备注" prop="name">
                <el-input v-model="ruleForm.remark" placeholder="请输入备注" />
            </el-form-item>
            <el-form-item label="广告类型" prop="region">
                <el-select v-model="ruleForm.type" placeholder="图片/视频">
                    <el-option label="图片" value="1" />
                    <el-option label="视频" value="2" />
                </el-select>
            </el-form-item>
            <div class="df">
                <el-upload
                 class="avatar-uploader"
                 action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
                >
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
             </el-upload>
             <span class="tip" v-if="ruleForm.type == '1'">
                图片：请上传，支持.jpg.jpeg.png格式，尺寸为1080*1920px 或 1920*1080px，小于1M。
             </span>
             <span class="tip" v-if="ruleForm.type == '2'">
                视频：请上传，支持.MP4.AVI.MOV格式， 比例为16：9 或 9：16， 小于10M。
             </span>
            </div>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <div>
                    <el-checkbox v-model="checked" label="" size="large" />
                    我已同意
                    <span class="statement">广告发布承诺声明</span>
                </div>
                <div>
                    <el-button @click="close()">取消</el-button>
                    <el-button type="primary" @click="submit(ruleFormRef)">确定</el-button>
                </div>
            </div>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import { defineProps,defineEmits,toRefs } from 'vue';
const emit = defineEmits(['close','submit']);
const props = defineProps<{ type:number,formData:any }>();
const { type, formData } = toRefs(props);
const comTitle = (type:number) => {
    if(type){
      return '编辑广告'
    }else{
      return '新增广告'
    }
  }
interface RuleForm {
  name: string,
  type:string,
  remark:string,
  url:''
}
const checked = ref(false)
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
  name: 'Hello',
  type:'1',
  remark:'',
  url:''
})
const rules = ref<FormRules<RuleForm>>({
  name: [ 
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
})
import type { UploadProps } from 'element-plus'

const imageUrl = ref('')

const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!)
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}
// 提交
const submit = async (formEl: FormInstance | undefined) => {
  await formEl.validate((valid:any, fields:any) => {
    if (valid) {
        emit('submit')
    } else {
      console.log('error submit!', fields)
    }
  })
}
// 取消
const close = () => {
    emit('close')
}
const show = ref(true)
</script>

<style lang="scss" scoped>
.df{
    display: flex;
    align-items: center;
}
.statement{
    color: blue;
}
.dialog-footer{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    font-size: 14px;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.tip{
    display: inline-block;
    width: 220px;
    margin-left: 20px;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>